#!/bin/bash

# ============================================
# 启动并验证 Nginx
# ============================================

set -e

echo "正在启动 Nginx..."

# 启动 Nginx
sudo systemctl start nginx

# 设置开机自启
sudo systemctl enable nginx

# 等待服务启动
sleep 2

# 检查状态
echo ""
echo "=== Nginx 服务状态 ==="
sudo systemctl status nginx --no-pager | head -n 15

# 检查端口监听
echo ""
echo "=== 端口监听状态 ==="
sudo netstat -tlnp | grep :80 || sudo ss -tlnp | grep :80

# 测试访问
echo ""
echo "=== 测试本地访问 ==="
curl -I http://localhost 2>&1 | head -5

# 检查防火墙
echo ""
echo "=== 防火墙状态 ==="
if command -v firewall-cmd &> /dev/null; then
    sudo firewall-cmd --list-all | grep -E "services:|http" || echo "防火墙未配置 HTTP"
fi

echo ""
echo "=== 部署信息 ==="
echo "访问地址: http://10.170.129.4"
echo "前端目录: /var/www/frontend"
echo "配置文件: /etc/nginx/conf.d/frontend.conf"
echo ""
echo "查看日志:"
echo "  sudo tail -f /var/log/nginx/frontend_access.log"
echo "  sudo tail -f /var/log/nginx/frontend_error.log"

